package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.TicketTag;
import cn.wolfcode.luowowo.article.mapper.TicketTagMapper;
import cn.wolfcode.luowowo.article.service.ITicketService;
import cn.wolfcode.luowowo.article.service.ITicketTagService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class TicketTagServiceImpl implements ITicketTagService {

    @Autowired
    private TicketTagMapper ticketTagMapper;

    @Reference
    private ITicketService ticketService;

    @Override
    public void save(TicketTag ticketTag) {
        ticketTagMapper.insert(ticketTag);
    }

    @Override
    public void delete(Long ticketTagId) {
        ticketTagMapper.deleteByPrimaryKey(ticketTagId);
    }

    @Override
    public void update(TicketTag ticketTag) {
        ticketTagMapper.updateByPrimaryKey(ticketTag);
    }

    @Override
    public TicketTag get(Long ticketTagId) {
        return ticketTagMapper.selectByPrimaryKey(ticketTagId);
    }

    @Override
    public List<TicketTag> list() {
        return ticketTagMapper.selectAll();
    }

    @Override
    public List<TicketTag> selectTagTOP8(Long destId) {
        List<Ticket> tickets = ticketService.listTicketByDestId(destId);
        /*Long[] ids = new Long[tickets.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = tickets.get(i).getId();
        }
        List<TicketTag> tags = ticketTagMapper.selectByTicketIdList(ids);*/
        List<TicketTag> tags = new ArrayList<>();
        List<Long> longs = new ArrayList<>();
        for (Ticket ticket : tickets) {
            List<TicketTag> ticketTags = ticketTagMapper.selectByTicketId(ticket.getId());
            for (TicketTag ticketTag : ticketTags) {
                if(!longs.contains(ticketTag.getId())){
                    tags.add(ticketTag);
                    longs.add(ticketTag.getId());
                }
            }
        }
        return tags.size() > 8 ? tags.subList(0, 8) : tags;
    }
}
